import { Global } from "../global/Global";
import { TextInput } from "../../../fl/controls/TextInput";
import { BaseButton } from "../../../fl/controls/BaseButton";
import { KeyCode } from "./KeyCode";
import { KeyEvent } from "./KeyEvent";
import { KeyData } from "./KeyData";
type int = number;
//class KeyBoardManager
    
    export  class KeyBoardManager /* flash.events.EventDispatcher */
    {
        constructor()
        {
            
            if (KeyBoardManager._instance != null) 
            {
                throw new Error("KeyBoardManager 不能实例化");
            }
            KeyBoardManager._instance = this;
            return;
        }

        public start(): void
        {
            KeyBoardManager._stage = Global.stage;
            if (KeyBoardManager._stage) 
            {
                this.addListener();
            }
            return;
        }

        public cancelListener(): void
        {
            if (KeyBoardManager._stage) 
            {
                KeyBoardManager._stage.removeEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.onKeyDownHandler);
                KeyBoardManager._stage.removeEventListener(flash.events.KeyboardEvent.KEY_UP, this.onKeyUpHandler);
                KeyBoardManager._stage.removeEventListener(flash.events.FocusEvent.MOUSE_FOCUS_CHANGE, this.onMouseFocusChangeHandler);
            }
            return;
        }

        public addListener(): void
        {
            if (KeyBoardManager._stage) 
            {
                KeyBoardManager._stage.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.onKeyDownHandler, false, 9999, false);
                KeyBoardManager._stage.addEventListener(flash.events.KeyboardEvent.KEY_UP, this.onKeyUpHandler, false, 9999, false);
                KeyBoardManager._stage.addEventListener(flash.events.FocusEvent.MOUSE_FOCUS_CHANGE, this.onMouseFocusChangeHandler, false, 9999, false);
                KeyBoardManager._stage.addEventListener(flash.events.Event.ACTIVATE, this.onActivateHandler, false, 9999, false);
            }
            return;
        }

        /* internal  */onActivateHandler(arg1: Object /* flash.events.Event */): void
        {
            KeyBoardManager._stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDownHandler, true, 9999, false);
            return;
        }

        /* internal  */onMouseLeaveHandler(arg1: Object /* flash.events.Event */): void
        {
            KeyBoardManager._stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDownHandler, true, 9999, false);
            return;
        }

        /* internal  */onMouseFocusChangeHandler(arg1: Object /* flash.events.FocusEvent */): void
        {
            if (arg1.relatedObject is flash.text.TextField) 
            {
                if ((arg1.relatedObject as Object /* flash.text.TextField */).type != flash.text.TextFieldType.INPUT) 
                {
                    this.changeImeEnable(false);
                }
                else 
                {
                    this.changeImeEnable(true);
                }
            }
            else if (typeof arg1.relatedObject === "textinput") 
            {
                this.changeImeEnable(true);
            }
            else if (typeof arg1.relatedObject === "basebutton") 
            {
                this.changeImeEnable(false);
            }
            else 
            {
                KeyBoardManager.ctrlKey == arg1.keyCode == KeyCode.CONTROL;
                KeyBoardManager.ShiftKey == arg1.shiftKey;
                KeyBoardManager._stage.focus = KeyBoardManager._stage;
                this.changeImeEnable(false);
            }
            return;
        }

        public changeImeEnable(arg1: Boolean): void
        {
            if (arg1 == flash.system.IME.enabled) 
            {
                return;
            }
            if (flash.system.Capabilities.hasIME) 
            {
                try 
                {
                    flash.system.IME.enabled = arg1;
                }
                catch (e)
                {
                };
            }
            return;
        }

        /* internal  */onMouseDownHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (KeyBoardManager.ctrlKey != arg1.ctrlKey) 
            {
                KeyBoardManager.ctrlKey = arg1.ctrlKey;
                if (arg1.ctrlKey) 
                {
                    this.dispatchKeyDownEvent(KeyCode.CONTROL, this.getKeyboardEvent(flash.events.KeyboardEvent.KEY_UP, KeyCode.CONTROL));
                }
                else 
                {
                    this.dispatchKeyUpEvent(KeyCode.CONTROL, this.getKeyboardEvent(flash.events.KeyboardEvent.KEY_DOWN, KeyCode.CONTROL));
                }
            }
            if (KeyBoardManager.ShiftKey != arg1.shiftKey) 
            {
                KeyBoardManager.ShiftKey = arg1.shiftKey;
                if (arg1.shiftKey) 
                {
                    this.dispatchKeyDownEvent(KeyCode.SHIFT, this.getKeyboardEvent(flash.events.KeyboardEvent.KEY_UP, KeyCode.SHIFT));
                }
                else 
                {
                    this.dispatchKeyUpEvent(KeyCode.SHIFT, this.getKeyboardEvent(flash.events.KeyboardEvent.KEY_DOWN, KeyCode.SHIFT));
                }
            }
            KeyBoardManager._stage.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
            return;
        }

        /* internal  */onKeyDownHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            KeyBoardManager.ctrlKey = arg1.ctrlKey;
            KeyBoardManager.ShiftKey = arg1.shiftKey;
            if (this._tempKeyCode == arg1.keyCode) 
            {
                return;
            }
            this._tempKeyCode = arg1.keyCode;
            if (arg1.keyCode != KeyCode.CONTROL) 
            {
                if (arg1.keyCode != KeyCode.SHIFT) 
                {
                    this.dispatchKeyDownEvent(arg1.keyCode, arg1, KeyBoardManager.ShiftKey);
                }
                else 
                {
                    KeyBoardManager.ShiftKey = true;
                    this.dispatchKeyDownEvent(KeyCode.SHIFT, arg1);
                }
            }
            else 
            {
                KeyBoardManager.ctrlKey = true;
                this.dispatchKeyDownEvent(KeyCode.CONTROL, arg1);
            }
            arg1.stopImmediatePropagation();
            return;
        }

        /* internal  */onKeyUpHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            if (this._tempKeyCode == arg1.keyCode) 
            {
                this._tempKeyCode = 0;
            }
            if (arg1.keyCode != KeyCode.CONTROL) 
            {
                if (arg1.keyCode != KeyCode.SHIFT) 
                {
                    this.dispatchKeyUpEvent(arg1.keyCode, arg1, KeyBoardManager.ShiftKey);
                }
                else 
                {
                    KeyBoardManager.ShiftKey = false;
                    this.dispatchKeyUpEvent(KeyCode.SHIFT, arg1);
                }
            }
            else 
            {
                KeyBoardManager.ctrlKey = false;
                this.dispatchKeyUpEvent(KeyCode.CONTROL, arg1);
            }
            arg1.stopImmediatePropagation();
            return;
        }

        /* internal  */dispatchKeyUpEvent(arg1: int, arg2: Object /* flash.events.KeyboardEvent */=null, arg3: Boolean=false): void
        {
            var loc1=KeyBoardManager.createKeyData(arg1, arg3);
            var loc2;
            (loc2 = new KeyEvent(KeyEvent.KEY_UP, loc1)).keyEvent = arg2;
            dispatchEvent(loc2);
            return;
        }

        /* internal  */dispatchKeyDownEvent(arg1: int, arg2: Object /* flash.events.KeyboardEvent */=null, arg3: Boolean=false): void
        {
            var loc1=KeyBoardManager.createKeyData(arg1, arg3);
            var loc2;
            (loc2 = new KeyEvent(KeyEvent.KEY_DOWN, loc1)).keyEvent = arg2;
            dispatchEvent(loc2);
            return;
        }

        /* internal  */getKeyboardEvent(arg1: string, arg2: uint): Object /* flash.events.KeyboardEvent */
        {
            return new Object /* flash.events.KeyboardEvent */(arg1, true, false, 0, arg2, 0, KeyBoardManager.ctrlKey, false, KeyBoardManager.ShiftKey);
        }

        public static get instance(): KeyBoardManager
        {
            if (KeyBoardManager._instance == null) 
            {
                KeyBoardManager._instance = new KeyBoardManager();
            }
            return KeyBoardManager._instance;
        }

        public static addkeys(arg1: Array<any>): void
        {
            var loc2=null;
            if (arg1 == null || arg1.length == 0) 
            {
                return;
            }
            if (arg1[0] as KeyData == null) 
            {
                throw new Error("KeyBoardManager.addkeys(value: Array<any>) value中不是KeyData类型");
            }
            var loc1=arg1.length;
            var loc3=0;
            while (loc3 < loc1) 
            {
                loc2 = arg1[loc3] as KeyData;
                KeyBoardManager._keyMap[loc2.keyCode] = loc2;
                ++loc3;
            }
            return;
        }

        public static createKeyData(arg1: uint, arg2: Boolean=false): KeyData
        {
            var loc1=KeyBoardManager.getKeyData(arg1, arg2);
            if (loc1 == null) 
            {
                loc1 = new KeyData(arg1, arg2);
                loc1.isKeyDown = false;
                KeyBoardManager._keyMap[loc1.key] = loc1;
            }
            return loc1;
        }

        public static getKeyData(arg1: uint, arg2: Boolean=false): KeyData
        {
            if (arg2) 
            {
                arg1 = arg1 + KeyCode.SHIFT_NUM;
            }
            return KeyBoardManager._keyMap[arg1];
        }

        
        {
            KeyBoardManager._keyMap = {};
        }

        private /* var */_tempKeyCode: uint=0;

        private /* var */_isDispatchEvent: Boolean=false;

        private static /* var */_keyMap: Object;

        private static /* var */_instance: KeyBoardManager;

        private static /* var */_stage: Object /* flash.display.Stage */;

        public static /* var */ctrlKey: Boolean;

        public static /* var */ShiftKey: Boolean;
    }
